//
//  VersionsController.m
//  Book Editor
//
//  Created by Chris Karr on 7/7/08.
//  Copyright 2008 Northwestern University. All rights reserved.
//

#import "VersionsController.h"

@implementation VersionsController

- (void) awakeFromNib
{
	[versions addObserver:self forKeyPath:@"selection" options:0 context:NULL];
	[versions addObserver:self forKeyPath:@"arrangedObjects" options:(NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld) context:NULL];
	
	[table setDoubleAction:@selector(openFromTable:)];
	[table setTarget:self];

	[table reloadData];
}

- (IBAction) openFile:(id) sender
{
	if ([[versions selectedObjects] count] > 0)
	{
		NSManagedObject * version = [[versions selectedObjects] lastObject];
	
		NSString * bookFilename = [version valueForKey:@"filename"];
	
		NSString * path = [book pathForFile:bookFilename];

		[[NSWorkspace sharedWorkspace] openFile:path];
	}
}

- (IBAction) openFromTable:(id) sender
{
	if ([[tableArray selectedObjects] count] > 0)
	{
		NSDictionary * version = [[tableArray selectedObjects] lastObject];
		
		NSString * bookFilename = [version valueForKey:@"filename"];
		
		if (bookFilename != nil)
		{
			NSString * path = [book pathForFile:bookFilename];
		
			[[NSWorkspace sharedWorkspace] openFile:path];
		}
	}
}

- (void) reveal:(NSString *) file
{
	NSLog (@"reveal %@", file);
	
	NSString * path = [book pathForFile:file];
	
	[[NSWorkspace sharedWorkspace] selectFile:path inFileViewerRootedAtPath:nil];
}

- (IBAction) revealFromTable:(id) sender
{
	if ([[tableArray selectedObjects] count] > 0)
	{
		NSDictionary * version = [[tableArray selectedObjects] lastObject];
		
		NSString * bookFilename = [version valueForKey:@"filename"];

		[self reveal:bookFilename];
	}
}

- (IBAction) revealFile:(id) sender
{
	if ([[versions selectedObjects] count] > 0)
	{
		NSManagedObject * version = [[versions selectedObjects] lastObject];
		
		NSString * bookFilename = [version valueForKey:@"filename"];

		[self reveal:bookFilename];
	}
}

- (void) observeValueForKeyPath:(NSString *) keyPath ofObject:(id) object change:(NSDictionary *) change context:(void *) context
{
	[self willChangeValueForKey:@"arrangedObjects"];

	if ([keyPath isEqual:@"selection"])
	{
		NSManagedObject * version = [[versions selectedObjects] lastObject];
	
		NSString * bookFilename = [version valueForKey:@"filename"];
	
		NSString * path = [book pathForFile:bookFilename];
	
		if (bookFilename != nil)
		{
			NSWorkspace * workspace = [NSWorkspace sharedWorkspace];
			NSString * type = nil;
			NSString * appName = nil;

			[workspace getInfoForFile:path application:&appName type:&type];
		
			NSImage * icon = [workspace iconForFileType:type];

			[imageView setImage:icon];
			[filename setStringValue:[NSString stringWithFormat:@"%@ (%@)", bookFilename, type]];
			[application setStringValue:[appName lastPathComponent]];

			NSDictionary * fileAttributes = [[NSFileManager defaultManager] fileAttributesAtPath:path traverseLink:YES];

			NSNumber * bytesSize = [fileAttributes valueForKey:NSFileSize];

			long fileSize = [bytesSize longValue];
		
			NSString * sizeString = [NSString stringWithFormat:@"%d bytes", fileSize, nil];
		
			if (fileSize >= 1024 * 1024)
				sizeString = [NSString stringWithFormat:@"%@ (%.2f MB)", sizeString, ((double) fileSize) / (1024 * 1024), nil];
			else if (fileSize >= 1024)
				sizeString = [NSString stringWithFormat:@"%@ (%.2f KB)", sizeString, ((double) fileSize) / 1024, nil];
		
			[size setStringValue:sizeString];
		}
		else
		{
			[imageView setImage:nil];
			[filename setStringValue:NSLocalizedString (@"NO FILE AVAILABLE", nil)];
			[application setStringValue:NSLocalizedString (@"NOT APPLICABLE", nil)];
			[size setStringValue:NSLocalizedString (@"NOT APPLICABLE", nil)];
		}
	}
	else
	{
		for (NSManagedObject * version in [versions arrangedObjects])
		{
			[version addObserver:self forKeyPath:@"edition" options:0 context:NULL];
			[version addObserver:self forKeyPath:@"format" options:0 context:NULL];
			[version addObserver:self forKeyPath:@"filename" options:0 context:NULL];
		}
	}

	[self didChangeValueForKey:@"arrangedObjects"];
}

- (void) setFile:(NSString *) path
{
	NSManagedObject * version = [[versions selectedObjects] lastObject];
	
	NSLog (@"set file '%@'", path);
	
	NSString * bookFilename = [version valueForKey:@"filename"];
	
	if (bookFilename != nil)
		[book removeFile:bookFilename];

	bookFilename = [book addFile:path];

	[version setValue:bookFilename forKey:@"filename"];
}

- (NSArray *) arrangedObjects
{
	NSMutableArray * objects = [NSMutableArray array];
	
	for (NSManagedObject * version in [versions arrangedObjects])
	{
		NSMutableDictionary * item = [NSMutableDictionary dictionary];
		
		if ([version valueForKey:@"format"] != nil)
			[item setValue:[version valueForKey:@"format"] forKey:@"format"];
		else
			[item setValue:NSLocalizedString (@"UNKNOWN FORMAT", nil) forKey:@"format"];
	
		if ([version valueForKey:@"edition"] != nil)
			[item setValue:[version valueForKey:@"edition"] forKey:@"edition"];
		else
			[item setValue:NSLocalizedString (@"UNKNOWN EDITION", nil) forKey:@"edition"];

		if ([version valueForKey:@"filename"] != nil)
		{
			[item setValue:[version valueForKey:@"filename"] forKey:@"filename"];

			NSString * path = [book pathForFile:[version valueForKey:@"filename"]];
			
			[item setValue:[[[NSWorkspace sharedWorkspace] iconForFile:path] TIFFRepresentation] forKey:@"icon"];
		}
		
		[objects addObject:item];
	}
	
	return objects;
}

- (IBAction) remove:(id) sender
{
	if ([[versions selectedObjects] count] > 0)
	{
		NSManagedObject * version = [[versions selectedObjects] lastObject];
		
		NSString * bookFilename = [version valueForKey:@"filename"];

		if (bookFilename != nil)
			[book removeFile:bookFilename];
		
		[versions remove:sender];
	}
}

@end
